package com.hos.db.create.ill;

import com.hos.db.entity.ill.IllBuildEntity;
import com.hos.utile.DLog;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;

import java.util.Date;
import java.util.List;

/**
 * 病历病人体格信息
 * Created by guom on 2018/10/15.
 */
public class IllBuildTable {
    public static String tableName = "pat_ill_builds";
    public static String tableIllId = "ill_id";//病例id
    public static String tableBuildHeat = "build_heat";//体温
    public static String tableHeartRate = "heart_rate";//心率
    public static String tableBuildBreathe = "build_breathe";//呼吸
    public static String tableBloodPressureShrink = "blood_pressure_shrink";//血压收缩
    public static String tableBloodPressureDiastole = "blood_pressure_diastole";//血压舒张
    public static String tableBuildMind = "build_mind";//神志
    public static String tableHeadTrait = "head_trait";//头部特征
    public static String tableHeadOther = "head_other";//头部其它
    public static String tableNeuroTrait = "neuro_trait";//神经特征
    public static String tableNeuroOther = "neuro_other";//神经其它
    public static String tableBuildNeck = "build_neck";//颈部
    public static String tableBuildChest = "build_chest";//胸部
    public static String tableBuildBelly = "build_belly";//腹部
    public static String tableBuildGroinTrait = "build_groin_trait";//腹股沟区部位
    public static String tableBuildGroinSymptom = "build_groin_symptom";//腹股沟区症状
    public static String tableUpperLimbTrait = "upper_limb_trait";//上肢部位
    public static String tableUpperLimbSymptom = "upper_limb_symptom";//上肢症状
    public static String tableUpperLimbExplain = "upper_limb_explain";//上肢描述
    public static String tableLowerLimbsTrait = "lower_limbs_trait";//下肢部位
    public static String tableLowerLimbsSymptom = "lower_limbs_symptom";//下肢症状
    public static String tableLowerLimbsExplain = "lower_limbs_explain";//下肢描述
    public static String tableBuildExplain = "build_explain";//补充说明
    public static String tableCreateTime = "create_time";// 病例创建时间
    //
    public static String careteTable = "create table  "
            + tableName + " ( "
            + tableIllId + "  varchar(200), "//主键
            + tableBuildHeat + " varchar(200), "
            + tableHeartRate + " varchar(200), "
            + tableBuildBreathe + " varchar(200), "
            + tableBloodPressureShrink + "  varchar(200), "
            + tableBloodPressureDiastole + "  varchar(200), "
            + tableBuildMind + "  varchar(200), "
            + tableHeadTrait + "  varchar(200), "
            + tableHeadOther + "  varchar(200), "
            + tableNeuroTrait + "  varchar(200), "
            + tableNeuroOther + "  varchar(200), "
            + tableBuildNeck + " varchar(200), "
            + tableBuildChest + " varchar(200), "
            + tableBuildBelly + " varchar(200), "
            + tableBuildGroinTrait + " varchar(200), "
            + tableBuildGroinSymptom + " varchar(200), "
            + tableUpperLimbTrait + " varchar(200), "
            + tableUpperLimbSymptom + " varchar(200), "
            + tableUpperLimbExplain + " varchar(200), "
            + tableLowerLimbsTrait + " varchar(200), "
            + tableLowerLimbsSymptom + " varchar(200), "
            + tableLowerLimbsExplain + " varchar(200), "
            + tableBuildExplain + " longText, "
            + tableCreateTime + " datetime "
            + "); ";

    public static boolean onInsert(JdbcTemplate jdbcTemplate, String illId, IllBuildEntity bean) {
        if (bean == null) {
            bean = new IllBuildEntity();
        }
        String sql = "INSERT INTO " + IllBuildTable.tableName
                + "("
                + IllBuildTable.tableIllId + ","
                + IllBuildTable.tableBuildHeat + ","
                + IllBuildTable.tableHeartRate + ","
                + IllBuildTable.tableBuildBreathe + ","
                + IllBuildTable.tableBloodPressureShrink + ","
                + IllBuildTable.tableBloodPressureDiastole + ","
                + IllBuildTable.tableBuildMind + ","
                + IllBuildTable.tableHeadTrait + ","
                + IllBuildTable.tableHeadOther + ","
                + IllBuildTable.tableNeuroTrait + ","
                + IllBuildTable.tableNeuroOther + ","
                + IllBuildTable.tableBuildNeck + ","
                + IllBuildTable.tableBuildChest + ","
                + IllBuildTable.tableBuildBelly + ","
                + IllBuildTable.tableBuildGroinTrait + ","
                + IllBuildTable.tableBuildGroinSymptom + ","
                + IllBuildTable.tableUpperLimbTrait + ","
                + IllBuildTable.tableUpperLimbSymptom + ","
                + IllBuildTable.tableUpperLimbExplain + ","
                + IllBuildTable.tableLowerLimbsTrait + ","
                + IllBuildTable.tableLowerLimbsSymptom + ","
                + IllBuildTable.tableLowerLimbsExplain + ","
                + IllBuildTable.tableBuildExplain + ","
                + IllBuildTable.tableCreateTime
                + ")"
                + " VALUES (?,?,?, ?,?,?, ?,?,?,?,?, ?,?,?, ?,?,?,?,?, ?,?,?, ?,?)";
        Object[] obj = new Object[]{illId, bean.getBuildHeat(), bean.getHeartRate(),
                bean.getBuildBreathe(), bean.getBloodPressureShrink(), bean.getBloodPressureDiastole(),
                bean.getBuildMind(), bean.getHeadTrait(), bean.getHeadOther(), bean.getNeuroTrait(),
                bean.getNeuroOther(), bean.getBuildNeck(), bean.getBuildChest(), bean.getBuildBelly(),
                bean.getBuildGroinTrait(), bean.getBuildGroinSymptom(), bean.getUpperLimbTrait(),
                bean.getUpperLimbSymptom(), bean.getUpperLimbExplain(), bean.getLowerLimbsTrait(),
                bean.getLowerLimbsSymptom(), bean.getLowerLimbsExplain(), bean.getBuildExplain(),
                new Date()};
        jdbcTemplate.update(sql, obj);
        return true;
    }

    //删除
    public static boolean onDlete(JdbcTemplate jdbcTemplate, String illId) {
        String sql = "DELETE  FROM " + IllBuildTable.tableName
                + " WHERE "
                + IllBuildTable.tableIllId + " = '" + illId + "'";
        int state = jdbcTemplate.update(sql);
        return state > 0;
    }

    //修改
    public static boolean onUpdate(JdbcTemplate jdbcTemplate, String illId, IllBuildEntity bean) {
        if (bean == null) {
            return true;
        }
        String update = "";
        String value = bean.getBuildHeat();
        if (value!=null) {
            update += " , " + IllBuildTable.tableBuildHeat + " = '" + value + "'";
        }
        value = bean.getHeartRate();
        if (value!=null) {
            update += " , " + IllBuildTable.tableHeartRate + " = '" + value + "'";
        }
        value = bean.getBuildBreathe();
        if (value!=null) {
            update += " , " + IllBuildTable.tableBuildBreathe + " = '" + value + "'";
        }
        value = bean.getBloodPressureShrink();
        if (value!=null) {
            update += " , " + IllBuildTable.tableBloodPressureShrink + " = '" + value + "'";
        }
        value = bean.getBloodPressureDiastole();
        if (value!=null) {
            update += " , " + IllBuildTable.tableBloodPressureDiastole + " = '" + value + "'";
        }
        value = bean.getBuildMind();
        if (value!=null) {
            update += " , " + IllBuildTable.tableBuildMind + " = '" + value + "'";
        }
        value = bean.getHeadTrait();
        if (value!=null) {
            update += " , " + IllBuildTable.tableHeadTrait + " = '" + value + "'";
        }
        value = bean.getHeadOther();
        if (value!=null) {
            update += " , " + IllBuildTable.tableHeadOther + " = '" + value + "'";
        }
        value = bean.getNeuroTrait();
        if (value!=null) {
            update += " , " + IllBuildTable.tableNeuroTrait + " = '" + value + "'";
        }
        value = bean.getNeuroOther();
        if (value!=null) {
            update += " , " + IllBuildTable.tableNeuroOther + " = '" + value + "'";
        }
        value = bean.getBuildNeck();
        if (value!=null) {
            update += " , " + IllBuildTable.tableBuildNeck + " = '" + value + "'";
        }
        value = bean.getBuildChest();
        if (value!=null) {
            update += " , " + IllBuildTable.tableBuildChest + " = '" + value + "'";
        }
        value = bean.getBuildBelly();
        if (value!=null) {
            update += " , " + IllBuildTable.tableBuildBelly + " = '" + value + "'";
        }
        value = bean.getBuildGroinTrait();
        if (value!=null) {
            update += " , " + IllBuildTable.tableBuildGroinTrait + " = '" + value + "'";
        }
        value = bean.getBuildGroinSymptom();
        if (value!=null) {
            update += " , " + IllBuildTable.tableBuildGroinSymptom + " = '" + value + "'";
        }
        value = bean.getUpperLimbTrait();
        if (value!=null) {
            update += " , " + IllBuildTable.tableUpperLimbTrait + " = '" + value + "'";
        }

        value = bean.getUpperLimbSymptom();
        if (value!=null) {
            update += " , " + IllBuildTable.tableUpperLimbSymptom + " = '" + value + "'";
        }
        value = bean.getUpperLimbExplain();
        if (value!=null) {
            update += " , " + IllBuildTable.tableUpperLimbExplain + " = '" + value + "'";
        }
        value = bean.getLowerLimbsTrait();
        if (value!=null) {
            update += " , " + IllBuildTable.tableLowerLimbsTrait + " = '" + value + "'";
        }
        value = bean.getLowerLimbsSymptom();
        if (value!=null) {
            update += " , " + IllBuildTable.tableLowerLimbsSymptom + " = '" + value + "'";
        }
        value = bean.getLowerLimbsExplain();
        if (value!=null) {
            update += " , " + IllBuildTable.tableLowerLimbsExplain + " = '" + value + "'";
        }
        value = bean.getBuildExplain();
        if (value!=null) {
            update += " , " + IllBuildTable.tableBuildExplain + " = '" + value + "'";
        }
        if (update.equals("")) {
            return true;
        }
        update = update.replaceFirst(" , ", "");
        String sql = "UPDATE " + IllBuildTable.tableName
                + " SET "
                + update
                + " WHERE "
                + IllBuildTable.tableIllId + " = '" + illId + "'";
        int s = jdbcTemplate.update(sql);
        return s > 0;
    }
    public static IllBuildEntity getResults(JdbcTemplate jdbcTemplate, String illId) {
        String sql = "SELECT * FROM " + IllBuildTable.tableName
                + " WHERE "
                + IllBuildTable.tableIllId + " = '" + illId + "'";
        IllBuildEntity entity = new IllBuildEntity();
        List<IllBuildEntity> ills = onQuery(jdbcTemplate, sql);
        if (ills != null && ills.size() > 0) {
            entity = ills.get(0);
        }
        return entity;
    }


    private static List<IllBuildEntity> onQuery(JdbcTemplate jdbcTemplate, String sql) {
        List<IllBuildEntity> ills = null;
        try {
            ills = jdbcTemplate.query(sql,
                    ParameterizedBeanPropertyRowMapper.newInstance(IllBuildEntity.class));
            DLog.e("查询成功：==========");
        } catch (Exception e) {
            DLog.e("查询失败：" + e.getMessage());
        }
        return ills;
    }
}
